package com.yiwenup.leetcode.top_interview_150;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 文件描述
 *
 * @Project struct-algorithm
 * @Package com.yiwenup.leetcode.top_interview_150
 * @Author yiwenup
 * @Date 2024-12-19 22:21:06
 * @Description https://leetcode.cn/problems/word-pattern/?envType=study-plan-v2&envId=top-interview-150
 */
public class No0290 {
    public boolean wordPattern(String pattern, String s) {
        String[] arr = s.split(" ");
        if (pattern.length() != arr.length) return false;

        Map<String, Character> map1 = new HashMap<>();
        Map<Character, String> map2 = new HashMap<>();
        for (int i = 0; i < arr.length; i++) {
            String c1 = arr[i];
            char c2 = pattern.charAt(i);
            if ((map1.containsKey(c1) && map1.get(c1) != c2) || (map2.containsKey(c2) && !Objects.equals(map2.get(c2), c1))) {
                return false;
            }
            map1.put(c1, c2);
            map2.put(c2, c1);
        }
        return true;
    }
}
